package com.ustc.hewei.detectbeats.detect;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
* @description: 心跳检测的主类
* @author hewei
* @date 2022/8/12 10:07
* @version 1.0
*/

@Slf4j
public class BeatsDetect {

    private final SendBeat sendBeat;

    // 心跳检测的间隔时间
    private final int detectDelayTime;

    private final ScheduledExecutorService scheduledExecutorService;

    public BeatsDetect(SendBeat sendBeat, int detectDelayTime) {
        this.sendBeat = sendBeat;
        this.detectDelayTime = detectDelayTime;
        scheduledExecutorService = Executors.newScheduledThreadPool(1);
    }

    // 开启发送心跳信号的定时循环任务
    public void start() {
        scheduledExecutorService.scheduleAtFixedRate(sendBeat::send, 0, detectDelayTime, TimeUnit.MILLISECONDS);
    }
}
